<?php
/**
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package databeans
* @subpackage datetime
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* time duration wrapper.
* stores duration as seconds.
*/
class duration {
/*------------------------------------------------------------------------------
                                                                   global config
------------------------------------------------------------------------------*/
    /**
    * @var internal duration count
    */
    var $seconds=0;
    /**
    * @var bool disable setting new (previously undeclared) members
    */
    //var $_databean_lock_structure=true;
    
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
    /**
    * constructor alias
    * @access
    */
    function duration($value=0) {
        $this->__construct($value);
    }/**/
    
    /**
    * "real" constructor
    * @access public
    * @param float $value duration, in seconds
    */
    function __construct($value=0) {
        if (is_numeric($value))
            $this->seconds=$value;
    }/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
    /**
    * return as seconds
    * @access public
    * @return float
    */
    function getAsSeconds() {
        return $this->seconds;
    }/**/
    
    /**
    * return as array of years, days, hours, seconds
    * @param float $sec seconds, use self if not provided
    * @access public
    * @return array
    */
    function getAsArray($sec=null) {
        $array=array();
        // getting self seconds if seconds not provided
        if (!is_numeric($sec) and $this)
            $sec=$this->seconds;
        // getting 
        $array['positive']=$sec>0;
        $sec=abs($sec);
        // getting and removing microseconds
        $array['microseconds']=($sec-floor($sec))*1000;
        $sec=floor($sec);
        // getting years
        $array['years']=intval(floor(duration::getAsYears($sec)));
        // removing years duration
        $sec=$sec % 31556926;
        // getting days
        $array['days']=intval(floor(duration::getAsDays($sec)));
        // removing days duration
        $sec=$sec % 86400;
        // getting hours
        $array['hours']=intval(floor(duration::getAsHours($sec)));
        // removing days duration
        $sec=$sec % 3600;
        // getting minutes
        $array['minutes']=intval(floor(duration::getAsMinutes($sec)));
        // removing minutes duration
        $sec=$sec % 60;
        // getting seconds
        $array['seconds']=intval(floor($sec));
        // removing seconds duration
        //echo $sec;
        return $array;
    }/**/
    
    /**
    * return as microseconds
    * may be used static
    * @access public
    * @param float $sec seconds, use self if not provided
    * @return float
    */
    function getAsMicroSeconds($sec=null) {
        // getting self seconds if seconds not provided
        if (!is_numeric($sec) and $this)
            $sec=$this->seconds;
        return $sec*1000;
    }
    
    /**
    * return as minutes
    * @param float $sec seconds, use self if not provided
    * @access public
    * @return float
    */
    function getAsMinutes($sec=null) {
        // getting self seconds if seconds not provided
        if (!is_numeric($sec) and $this)
            $sec=$this->seconds;
        return $sec?$sec/60:0;
    }/**/
    
    /**
    * ge tas hours
    * @param float $sec seconds, use self if not provided
    * @access public
    * @return float
    */
    function getAsHours($sec=null) {
        // getting self seconds if seconds not provided
        if (!is_numeric($sec) and $this)
            $sec=$this->seconds;
        return $sec?$sec/3600:0;
    }/**/
    
    /**
    * return as days
    * @param float $sec seconds, use self if not provided
    * @access public
    * @return float
    */
    function getAsDays($sec=null) {
        // getting self seconds if seconds not provided
        if (!is_numeric($sec) and $this)
            $sec=$this->seconds;
        return $sec?$sec/86400:0;
    }/**/
    
    /**
    * return as years
    * @param float $sec seconds, use self if not provided
    * @access public
    * @return float
    */
    function getAsYears($sec=null) {
        // getting self seconds if seconds not provided
        if (!is_numeric($sec) and $this)
            $sec=$this->seconds;
        return $sec?$sec/31556926:0;
    }/**/
    
    /**
    * add years to duration
    * @param int $value
    * @access public
    * @return bool
    */
    function addYears($value=0) {
        $value=duration::tointeger($value);
        $sec=$value*31556926;
        $this->seconds+=$sec;
        return true;
    }/**/
    
    /**
    * add days to duration
    * @param int $value
    * @access public
    * @return bool
    */
    function addDays($value=0) {
        $value=duration::tointeger($value);
        $sec=$value*86400;
        $this->seconds+=$sec;
        return true;
    }/**/
    
    /**
    * add hours to duration
    * @param int $value
    * @access public
    * @return bool
    */
    function addHours($value=0) {
        $value=duration::tointeger($value);
        $sec=$value*3600;
        $this->seconds+=$sec;
        return true;
    }/**/
    
    /**
    * add minutes to duration
    * @param int $value
    * @access public
    * @return bool
    */
    function addMinutes($value=0) {
        $value=duration::tointeger($value);
        $sec=$value*60;
        $this->seconds+=$sec;
        return true;
    }/**/
    
    /**
    * add seconds to duration
    * @param int $value
    * @access public
    * @return bool
    */
    function addSeconds($value=0) {
        $value=duration::tointeger($value);
        $sec=$value;
        $this->seconds+=$sec;
        return true;
    }/**/
    
    /**
    * add days to duration
    * @param int $value
    * @access public
    * @return bool
    */
    function addMicroSeconds($value=0) {
        $value=duration::tointeger($value);
        $sec=$value?$value/1000:0;
        $this->seconds+=$sec;
        return true;
    }/**/
    
    /**
    * formats some value to integer
    * @param mixed $val
    * @access private
    * @return int
    */
    function toInteger($val=null) {
        if (is_int($val))
            return $val;
        else if (is_null($val))
            return 0;
        else if (is_string($val) and ereg('^[0-9.]$'))
            return intval($val);
        
        return 0;
    }/**/
}
?>
